package com.anti.commom.core.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2020-10-24
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
    private static Pattern patternMobile = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
    private static Pattern patternEmail = Pattern.compile("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\\.([a-zA-Z0-9_-])+)+$");

    public StringUtils() {
    }

    public static boolean isEmail(CharSequence email) {
        Matcher m = patternEmail.matcher(email);
        return m.matches();
    }

    public static boolean isMobile(CharSequence mobiles) {
        Matcher m = patternMobile.matcher(mobiles);
        return m.matches();
    }

    public static String fromCamelCase(String input, char separator) {
        int length = input.length();
        StringBuilder result = new StringBuilder(length * 2);
        int resultLength = 0;
        boolean prevTranslated = false;

        for(int i = 0; i < length; ++i) {
            char c = input.charAt(i);
            if (i > 0 || c != separator) {
                if (Character.isUpperCase(c)) {
                    if (!prevTranslated && resultLength > 0 && result.charAt(resultLength - 1) != separator) {
                        result.append(separator);
                        ++resultLength;
                    }

                    c = Character.toLowerCase(c);
                    prevTranslated = true;
                } else {
                    prevTranslated = false;
                }

                result.append(c);
                ++resultLength;
            }
        }

        return resultLength > 0 ? result.toString() : input;
    }

    public static String toCamelCase(String input, boolean firstCharUppercase, char separator) {
        int length = input.length();
        input = input.toLowerCase();
        StringBuilder sb = new StringBuilder(length);
        boolean upperCase = firstCharUppercase;

        for(int i = 0; i < length; ++i) {
            char ch = input.charAt(i);
            if (ch == separator) {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(ch));
                upperCase = false;
            } else {
                sb.append(ch);
            }
        }

        return sb.toString();
    }

    public static String toCamelCase(String input) {
        return toCamelCase(input, false, '_');
    }

    public static String toSafeString(Object value) {
        return value == null ? "" : value.toString();
    }

    public static String toLowerCase(String s) {
        if (s == null) {
            return null;
        } else {
            StringBuilder sb = null;
            int len = s.length();

            for(int i = 0; i < len; ++i) {
                char c = s.charAt(i);
                if (c > 127) {
                    return s.toLowerCase();
                }

                if (c >= 'A' && c <= 'Z') {
                    if (sb == null) {
                        sb = new StringBuilder(s);
                    }

                    sb.setCharAt(i, (char)(c + 32));
                }
            }

            if (sb == null) {
                return s;
            } else {
                return sb.toString();
            }
        }
    }

    public static String toUpperCase(String s) {
        if (s == null) {
            return null;
        } else {
            StringBuilder sb = null;

            for(int i = 0; i < s.length(); ++i) {
                char c = s.charAt(i);
                if (c > 127) {
                    return s.toUpperCase();
                }

                if (c >= 'a' && c <= 'z') {
                    if (sb == null) {
                        sb = new StringBuilder(s);
                    }

                    sb.setCharAt(i, (char)(c - 32));
                }
            }

            if (sb == null) {
                return s;
            } else {
                return sb.toString();
            }
        }
    }

    public static boolean startsWithChar(String s, char c) {
        return s.length() != 0 && s.charAt(0) == c;
    }

    public static boolean endsWithChar(String s, char c) {
        return s.length() != 0 && s.charAt(s.length() - 1) == c;
    }

    public static String surround(String str, char c, boolean prefix, boolean suffix) {
        int len = str.length();
        int newlen = len;
        if (prefix) {
            newlen = len + 1;
        }

        if (suffix) {
            ++newlen;
        }

        char[] buf = new char[newlen];
        int pos = 0;
        if (prefix) {
            buf[pos++] = c;
        }

        str.getChars(0, len, buf, pos);
        if (suffix) {
            buf[len + pos] = c;
        }

        return new String(buf);
    }

    public static String surround(String str, String fix, boolean prefix, boolean suffix) {
        int len = str.length();
        int newlen = len;
        int fixlen = fix.length();
        if (prefix) {
            newlen = len + fixlen;
        }

        if (suffix) {
            newlen += fixlen;
        }

        char[] buf = new char[newlen];
        int pos = 0;
        if (prefix) {
            fix.getChars(0, fixlen, buf, 0);
            pos += fixlen;
        }

        str.getChars(0, len, buf, pos);
        if (suffix) {
            fix.getChars(0, fixlen, buf, pos + len);
        }

        return new String(buf);
    }

    public static String surroundIfMissing(String str, char c) {
        boolean prefix = false;
        boolean suffix = false;
        if (!startsWithChar(str, c)) {
            prefix = true;
        }

        if (!endsWithChar(str, c)) {
            suffix = true;
        }

        return surround(str, c, prefix, suffix);
    }

    public static String surroundIfMissing(String str, String fix) {
        boolean prefix = false;
        boolean suffix = false;
        if (!str.startsWith(fix)) {
            prefix = true;
        }

        if (!str.endsWith(fix)) {
            suffix = true;
        }

        return surround(str, fix, prefix, suffix);
    }

    public static boolean isLetter(char c) {
        int k = 128;
        return c / k == 0;
    }

    public static int lengthb(String cs) {
        if (cs == null) {
            return 0;
        } else {
            char[] c = cs.toCharArray();
            int len = 0;
            char[] arr$ = c;
            int len$ = c.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                char aC = arr$[i$];
                ++len;
                if (!isLetter(aC)) {
                    ++len;
                }
            }

            return len;
        }
    }
//
//    public static void main(String[] args) {
//        String[] test = splitPreserveAllTokens("code,ddd", ",");
//        int i = 0;
//        String[] arr$ = test;
//        int len$ = test.length;
//
//        for(int i$ = 0; i$ < len$; ++i$) {
//            String v = arr$[i$];
//            System.out.println(i++ + ":" + v);
//        }
//
//        System.out.println(toCamelCase("user_name_id"));
//        System.out.println(join(new Serializable[]{"A1", "我", 2, 3.0D, "X", ".", "com"}));
//        System.out.println(replaceChars(".=.=.=", '=', 'A'));
//        System.out.println(remove(".=.=.=", '='));
//        System.out.println(lengthb("我Abcd祖国12345"));
//        System.out.println(repeat("?", ",", 10));
//    }
}
